<?php

namespace app\shops\controller;


use think\Session;
use think\Db;
use think\Model;

/**
 * 营销管理
 */
class Market extends Base
{

    /*
     * 活动列表页展示
     * wangtiantian
     */
    public function index()
    {

        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize = input('limit') ? input('limit') : $conf['paginate']['list_rows'];

        // 查询数据
        $model    = Db::name('shops_market_enlist');
        $list     = $model->where('is_del', 0)
                          ->where('shop_id',$this->shop_id)
                          ->order("id desc")->paginate($pageSize);
//        $aa = $list->toArray();
//        $res=$aa['data'];
//        foreach ($res  as  $k=> $v){
//            $res[$k]['create_time'] = date('Y-m-d H:s',$v['create_time']);
//        }

        $this->assign('total', $list->total());
        $this->assign('page', $list->render());
        $this->assign('list', $list);
        return $this->fetch();



    }

    /**
     * @throws
     * 添加报名优惠
     * @return [type] [description]
     */

    public function paymanager()
    {

        if (request()->isAjax()) {


            $d = input('');
            // 入库数据
            $data['title']          = $d['title'];
            $data['start_time']     = $d['start_time'];
            $data['end_time']       = $d['end_time'];


            $data['is_red_packet']  = $d['is_red_packet'];


            $data['pay_money']      = $d['pay_money'];
            $data['red_min']        = $d['red_min'];
            $data['red_max']        = $d['red_max'];
            $data['text']           = $d['text'];
            $data['banner1_url']    = $d['banner1_url'];
            $data['banner2_url']    = $d['banner2_url'];
            $data['banner3_url']    = $d['banner3_url'];
            $data['banner4_url']    = $d['banner4_url'];
            $data['skin_id']        = $d['skin_id'];
            $data['music_id']       = $d['music_id'];
            $data['shop_id']        = $this->shop_id;
            $data['create_time']    = time();
            $data['is_del']         = 0;


            $res = Db::name('shops_market_enlist')->insertGetId($data);
            if ($res) {
                $this->success('成功');
            } else {
                $this->error('失败');
            }
        }


        $model   = Db::name('music');
        $list    = $model
                    ->where('is_del', 0)
        //           ->where('type',0)
                     ->select();
        $this->assign('list', $list);

        $model1 = Db::name('skin');
        $list1 = $model1->where('is_del', 0)->where('is_show', 1)->select();
        $this->assign('list1', $list1);

        $sid = reset($list1);
        $mid = reset($list);

        $this->assign('mid',$mid['id']);
        $this->assign('sid',$sid['id']);
        return $this->fetch();
    }


    /*
     * wangtiantian
     * 删除皮肤
     */
    public function del()
    {
        $id = input('id');
        if (request()->isAjax()) {

            $res = Db::name('shops_market_enlist')->where('id', $id)->update(['is_del' => 1]);
            if ($res) {
                $this->success("删除成功");
            } else {
                $this->error("删除失败");
            }
        }
    }


    /**
     * @throws
     * 编辑皮肤
     * wangtiantian
     */
    public function edit()
    {
        $id = input('id');
        $mo = Db::name('shops_market_enlist');


        // 更新数据
        if (request()->isPost()) {

            $data = input();

            $data['create_time'] = time();
            $data['is_del']      = 0;

            $ret = $mo->where('id', $id)->update($data);
            if ($ret) {
                $this->success("更新成功");
            } else {
                $this->error("更新失败");
            }

        }

        $model = Db::name('music');
        $list = $model
            ->where('is_del', 0)
            ->select();
        $this->assign('list', $list);

        $model1 = Db::name('skin');
        $list1 = $model1->where('is_del', 0)->where('type', 0)->select();
        $this->assign('list1', $list1);
        // 详情
        $info = $mo->where('id', $id)->find();
        $this->assign('info', $info);

        return $this->fetch();
    }


    public function qr(){

        if (request()->isAjax()){
            //活动id
            session::set("shop_id",input('id'));
            if(session::get("shop_id")){
                $this->success();
            }
            $this->error();
        }

        $this->assign('shop_id',session::get("shop_id"));
        return $this->fetch('index');
    }

    public function qrcode()
    {
        // 活动id
        $shop_id = session::get("shop_id");

        // 二维码链接
        $url_arr['qr_s']  = request()->domain() . url("store/market/index",['id'=>$shop_id]);

        $this->assign('url_arr',$url_arr);

        return $this->fetch();
    }



}
